<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python documentation Index' />
<link rel="first" href="lib.html" title='Python library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="SMTP-example.html" />
<link rel="prev" href="module-smtplib.html" />
<link rel="parent" href="module-smtplib.html" />
<link rel="next" href="SMTP-example.html" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name='aesop' content='information' />
<title>18.13.1 SMTP Objects </title>
</head>
<body>
<div class="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="18.13 smtplib  "
  href="module-smtplib.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="18.13 smtplib  "
  href="module-smtplib.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="18.13.2 smtp Example"
  href="SMTP-example.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-smtplib.html">18.13 smtplib  </a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="module-smtplib.html">18.13 smtplib  </a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="SMTP-example.html">18.13.2 SMTP Example</a>
</div>
<hr /></div>
</div>
<!--End of Navigation Panel-->

<h2><a name="SECTION00201310000000000000000"></a><a name="SMTP-objects"></a>
<br>
18.13.1 SMTP Objects 
</h2>

<p>
An <tt class="class">SMTP</tt> instance has the following methods:

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-4210' xml:id='l2h-4210' class="method">set_debuglevel</tt></b>(</nobr></td>
  <td><var>level</var>)</td></tr></table></dt>
<dd>
Set the debug output level.  A true value for <var>level</var> results in
debug messages for connection and for all messages sent to and
received from the server.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-4211' xml:id='l2h-4211' class="method">connect</tt></b>(</nobr></td>
  <td><var></var><big>[</big><var>host</var><big>[</big><var>, port</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Connect to a host on a given port.  The defaults are to connect to the
local host at the standard SMTP port (25).
If the hostname ends with a colon ("<tt class="character">:</tt>") followed by a
number, that suffix will be stripped off and the number interpreted as
the port number to use.
This method is automatically invoked by the constructor if a
host is specified during instantiation.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-4212' xml:id='l2h-4212' class="method">docmd</tt></b>(</nobr></td>
  <td><var>cmd, </var><big>[</big><var>, argstring</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Send a command <var>cmd</var> to the server.  The optional argument
<var>argstring</var> is simply concatenated to the command, separated by a
space.

<p>
This returns a 2-tuple composed of a numeric response code and the
actual response line (multiline responses are joined into one long
line.)

<p>
In normal operation it should not be necessary to call this method
explicitly.  It is used to implement other methods and may be useful
for testing private extensions.

<p>
If the connection to the server is lost while waiting for the reply,
<tt class="exception">SMTPServerDisconnected</tt> will be raised.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-4213' xml:id='l2h-4213' class="method">helo</tt></b>(</nobr></td>
  <td><var></var><big>[</big><var>hostname</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Identify yourself to the SMTP server using "<tt class="samp">HELO</tt>".  The hostname
argument defaults to the fully qualified domain name of the local
host.

<p>
In normal operation it should not be necessary to call this method
explicitly.  It will be implicitly called by the <tt class="method">sendmail()</tt>
when necessary.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-4214' xml:id='l2h-4214' class="method">ehlo</tt></b>(</nobr></td>
  <td><var></var><big>[</big><var>hostname</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Identify yourself to an ESMTP server using "<tt class="samp">EHLO</tt>".  The hostname
argument defaults to the fully qualified domain name of the local
host.  Examine the response for ESMTP option and store them for use by
<tt class="method">has_extn()</tt>.

<p>
Unless you wish to use <tt class="method">has_extn()</tt> before sending
mail, it should not be necessary to call this method explicitly.  It
will be implicitly called by <tt class="method">sendmail()</tt> when necessary.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-4215' xml:id='l2h-4215' class="method">has_extn</tt></b>(</nobr></td>
  <td><var>name</var>)</td></tr></table></dt>
<dd>
Return <tt class="constant">True</tt> if <var>name</var> is in the set of SMTP service
extensions returned by the server, <tt class="constant">False</tt> otherwise.
Case is ignored.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-4216' xml:id='l2h-4216' class="method">verify</tt></b>(</nobr></td>
  <td><var>address</var>)</td></tr></table></dt>
<dd>
Check the validity of an address on this server using SMTP "<tt class="samp">VRFY</tt>".
Returns a tuple consisting of code 250 and a full <a class="rfc" id='rfcref-103178' xml:id='rfcref-103178'
href="http://www.faqs.org/rfcs/rfc822.html">RFC 822</a> address
(including human name) if the user address is valid. Otherwise returns
an SMTP error code of 400 or greater and an error string.

<p>
<span class="note"><b class="label">Note:</b>
Many sites disable SMTP "<tt class="samp">VRFY</tt>" in order to foil spammers.</span>
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-4217' xml:id='l2h-4217' class="method">login</tt></b>(</nobr></td>
  <td><var>user, password</var>)</td></tr></table></dt>
<dd>
Log in on an SMTP server that requires authentication.
The arguments are the username and the password to authenticate with.
If there has been no previous "<tt class="samp">EHLO</tt>" or "<tt class="samp">HELO</tt>" command this
session, this method tries ESMTP "<tt class="samp">EHLO</tt>" first.
This method will return normally if the authentication was successful,
or may raise the following exceptions:

<p>
<dl>
<dt><strong><tt class="exception">SMTPHeloError</tt></strong></dt>
<dd>The server didn't reply properly to the "<tt class="samp">HELO</tt>" greeting.
  
</dd>
<dt><strong><tt class="exception">SMTPAuthenticationError</tt></strong></dt>
<dd>The server didn't accept the username/password combination.
  
</dd>
<dt><strong><tt class="exception">SMTPException</tt></strong></dt>
<dd>No suitable authentication method was found.
</dd>
</dl>
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-4218' xml:id='l2h-4218' class="method">starttls</tt></b>(</nobr></td>
  <td><var></var><big>[</big><var>keyfile</var><big>[</big><var>, certfile</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Put the SMTP connection in TLS (Transport Layer Security) mode.  All
SMTP commands that follow will be encrypted.  You should then call
<tt class="method">ehlo()</tt> again.

<p>
If <var>keyfile</var> and <var>certfile</var> are provided, these are passed to
the <tt class="module"><a href="module-socket.html">socket</a></tt> module's <tt class="function">ssl()</tt> function.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-4219' xml:id='l2h-4219' class="method">sendmail</tt></b>(</nobr></td>
  <td><var>from_addr, to_addrs, msg</var><big>[</big><var>,
                             mail_options, rcpt_options</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Send mail.  The required arguments are an <a class="rfc" id='rfcref-103180' xml:id='rfcref-103180'
href="http://www.faqs.org/rfcs/rfc822.html">RFC 822</a> from-address
string, a list of <a class="rfc" id='rfcref-103182' xml:id='rfcref-103182'
href="http://www.faqs.org/rfcs/rfc822.html">RFC 822</a> to-address strings (a bare string will be
treated as a list with 1 address), and a message string.  The caller
may pass a list of ESMTP options (such as "<tt class="samp">8bitmime</tt>") to be used
in "<tt class="samp">MAIL FROM</tt>" commands as <var>mail_options</var>.  ESMTP options
(such as "<tt class="samp">DSN</tt>" commands) that should be used with all "<tt class="samp">RCPT</tt>"commands can be passed as <var>rcpt_options</var>.  (If you need to use
different ESMTP options to different recipients you have to use the
low-level methods such as <tt class="method">mail</tt>, <tt class="method">rcpt</tt> and
<tt class="method">data</tt> to send the message.)

<p>
<span class="note"><b class="label">Note:</b>
The <var>from_addr</var> and <var>to_addrs</var> parameters are
used to construct the message envelope used by the transport agents.
The <tt class="class">SMTP</tt> does not modify the message headers in any way.</span>

<p>
If there has been no previous "<tt class="samp">EHLO</tt>" or "<tt class="samp">HELO</tt>" command this
session, this method tries ESMTP "<tt class="samp">EHLO</tt>" first. If the server does
ESMTP, message size and each of the specified options will be passed
to it (if the option is in the feature set the server advertises).  If
"<tt class="samp">EHLO</tt>" fails, "<tt class="samp">HELO</tt>" will be tried and ESMTP options
suppressed.

<p>
This method will return normally if the mail is accepted for at least
one recipient. Otherwise it will throw an exception.  That is, if this
method does not throw an exception, then someone should get your mail.
If this method does not throw an exception, it returns a dictionary,
with one entry for each recipient that was refused.  Each entry
contains a tuple of the SMTP error code and the accompanying error
message sent by the server.

<p>
This method may raise the following exceptions:

<p>
<dl>
<dt><strong><tt class="exception">SMTPRecipientsRefused</tt></strong></dt>
<dd>All recipients were refused.  Nobody got the mail.  The
<tt class="member">recipients</tt> attribute of the exception object is a dictionary
with information about the refused recipients (like the one returned
when at least one recipient was accepted).

<p>
</dd>
<dt><strong><tt class="exception">SMTPHeloError</tt></strong></dt>
<dd>The server didn't reply properly to the "<tt class="samp">HELO</tt>" greeting.

<p>
</dd>
<dt><strong><tt class="exception">SMTPSenderRefused</tt></strong></dt>
<dd>The server didn't accept the <var>from_addr</var>.

<p>
</dd>
<dt><strong><tt class="exception">SMTPDataError</tt></strong></dt>
<dd>The server replied with an unexpected error code (other than a refusal
of a recipient).
</dd>
</dl>

<p>
Unless otherwise noted, the connection will be open even after
an exception is raised.

<p>
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-4220' xml:id='l2h-4220' class="method">quit</tt></b>(</nobr></td>
  <td><var></var>)</td></tr></table></dt>
<dd>
Terminate the SMTP session and close the connection.
</dl>

<p>
Low-level methods corresponding to the standard SMTP/ESMTP commands
"<tt class="samp">HELP</tt>", "<tt class="samp">RSET</tt>", "<tt class="samp">NOOP</tt>", "<tt class="samp">MAIL</tt>", "<tt class="samp">RCPT</tt>", and
"<tt class="samp">DATA</tt>" are also supported.  Normally these do not need to be
called directly, so they are not documented here.  For details,
consult the module code.

<p>

<div class="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="18.13 smtplib  "
  href="module-smtplib.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="18.13 smtplib  "
  href="module-smtplib.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="18.13.2 smtp Example"
  href="SMTP-example.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-smtplib.html">18.13 smtplib  </a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="module-smtplib.html">18.13 smtplib  </a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="SMTP-example.html">18.13.2 SMTP Example</a>
</div>
</div>
<hr />
<span class="release-info">Release 2.5.1, documentation updated on 18th April, 2007.</span>
</div>
<!--End of Navigation Panel-->
<address>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</address>
</body>
</html>
